# SqlToy 自动填充功能测试脚本 (PowerShell版本)
# 用于测试Creator和Updater字段的自动填充功能

$API_BASE = "http://localhost:8080/api"

Write-Host "===================================" -ForegroundColor Green
Write-Host "SqlToy 自动填充功能测试" -ForegroundColor Green
Write-Host "===================================" -ForegroundColor Green

Write-Host ""
Write-Host "1. 测试创建用户（Creator和Updater自动填充）" -ForegroundColor Yellow
Write-Host "-----------------------------------" -ForegroundColor Yellow

try {
    # 创建用户
    $createResponse = Invoke-RestMethod -Uri "$API_BASE/user?name=测试用户&age=25" -Method POST
    Write-Host "创建用户响应:" -ForegroundColor Cyan
    $createResponse | ConvertTo-Json -Depth 3
    
    if ($createResponse.success) {
        $userId = $createResponse.data.id
        Write-Host "创建的用户ID: $userId" -ForegroundColor Green
        
        Write-Host ""
        Write-Host "2. 测试查询用户（验证Creator和Updater字段）" -ForegroundColor Yellow
        Write-Host "-----------------------------------" -ForegroundColor Yellow
        
        # 查询用户
        $queryResponse = Invoke-RestMethod -Uri "$API_BASE/user/$userId" -Method GET
        Write-Host "查询用户响应:" -ForegroundColor Cyan
        $queryResponse | ConvertTo-Json -Depth 3
        
        Write-Host ""
        Write-Host "Creator字段: $($queryResponse.data.creator)" -ForegroundColor Green
        Write-Host "Updater字段: $($queryResponse.data.updater)" -ForegroundColor Green
        
        Write-Host ""
        Write-Host "3. 测试更新用户（Updater字段自动更新）" -ForegroundColor Yellow
        Write-Host "-----------------------------------" -ForegroundColor Yellow
        
        # 更新用户
        $updateResponse = Invoke-RestMethod -Uri "$API_BASE/user/$userId?name=更新后的用户名" -Method PUT
        Write-Host "更新用户响应:" -ForegroundColor Cyan
        $updateResponse | ConvertTo-Json -Depth 3
        
        Write-Host ""
        Write-Host "4. 再次查询用户（验证Updater字段被更新）" -ForegroundColor Yellow
        Write-Host "-----------------------------------" -ForegroundColor Yellow
        
        # 再次查询用户
        $finalQueryResponse = Invoke-RestMethod -Uri "$API_BASE/user/$userId" -Method GET
        Write-Host "最终查询响应:" -ForegroundColor Cyan
        $finalQueryResponse | ConvertTo-Json -Depth 3
        
        Write-Host ""
        Write-Host "更新后Creator字段: $($finalQueryResponse.data.creator)" -ForegroundColor Green
        Write-Host "更新后Updater字段: $($finalQueryResponse.data.updater)" -ForegroundColor Green
        
        Write-Host ""
        Write-Host "5. 测试创建订单（Creator和Updater自动填充）" -ForegroundColor Yellow
        Write-Host "-----------------------------------" -ForegroundColor Yellow
        
        # 创建订单（注意：可能会因为order表不存在而失败）
        try {
            $orderResponse = Invoke-RestMethod -Uri "$API_BASE/order?userId=$userId&amount=99.99" -Method POST
            Write-Host "创建订单响应:" -ForegroundColor Cyan
            $orderResponse | ConvertTo-Json -Depth 3
        } catch {
            Write-Host "创建订单失败（可能是因为order表不存在）:" -ForegroundColor Red
            Write-Host $_.Exception.Message -ForegroundColor Red
        }
        
    } else {
        Write-Host "用户创建失败，跳过后续测试" -ForegroundColor Red
    }
    
} catch {
    Write-Host "API调用失败:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
}

Write-Host ""
Write-Host "===================================" -ForegroundColor Green
Write-Host "测试完成" -ForegroundColor Green
Write-Host "===================================" -ForegroundColor Green

Write-Host ""
Write-Host "测试总结:" -ForegroundColor Cyan
Write-Host "- 插入数据时会自动填充creator和updater字段为当前用户" -ForegroundColor White
Write-Host "- 更新数据时会自动更新updater字段，creator字段保持不变" -ForegroundColor White
Write-Host "- 所有自动填充功能都是通过UnifyFieldsAutoFillHandler实现的" -ForegroundColor White